package com.gxa.wmcrm.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gxa.wmcrm.entity.Department;
import com.gxa.wmcrm.service.BumenService;
import com.gxa.wmcrm.service.Impl.BumenServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@WebServlet(name = "AddServlet", urlPatterns = "/AddServlet")
public class AddServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }


    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json; charset=utf-8");

        //后台接收JSON数据
        InputStream inputStream = request.getInputStream();
        InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8");
        // 字符缓冲区
        char[] buff = new char[1024];
        int length = 0;
        String jsonstr = null;
        while ((length = reader.read(buff)) != -1) {
            // 通过String的构造方法将 字符缓冲区 转化为一个String
            jsonstr = new String(buff, 0, length);
            System.out.println("--jsonstr--"+jsonstr);
            //jsonstr = {login: login, pwd: pwd, code: code, AdminCode: AdminCode};
        }
        // 通过 FastJSON方法 将JSON结构的字符串转为 map
        Map<String, Object> parameters = JSONObject.parseObject(jsonstr);
        String depotId = (String) parameters.get("depotId");
        String depotName = (String) parameters.get("depotName");
        String depotSetTime = (String) parameters.get("depotSetTime");
        String depotDescribe = (String) parameters.get("depotDescribe");

        System.out.println("-- depotId -- : " + depotId);
        System.out.println("-- depotName -- : " + depotName);
        System.out.println("-- depotSetTime -- : " + depotSetTime);
        System.out.println("-- depotDescribe -- : " + depotDescribe);


        Department bumen =new Department();
        bumen.setDepotId(Integer.valueOf(depotId));
        bumen.setDepotName(depotName);
        Date date = java.sql.Date.valueOf(depotSetTime);
        bumen.setDepotSetTime(date);
        bumen.setDepotDescribe(depotDescribe);
        BumenService bumenService = new BumenServiceImpl();
        int action =  bumenService.addBumen(bumen);


        PrintWriter out = response.getWriter();
        Map<String,Object> map = new HashMap<>();
        String message;
            if (action == 1) {
                // 准备返回数据 - Map
                map.put("Status","success");
                // map转字符串
                message = JSON.toJSONString(map);
                out.print(message);
            } else {
                map.put("Status","error");
                message = JSON.toJSONString(map);
                out.print(message);
            }
        }
    }



